package com.hangzhou.xyj.net.http;

import android.util.Log;

import com.hangzhou.xyj.net.handler.base.BaseHttpHandler;
import com.hangzhou.xyj.net.http.base.BaseHttpLoader;
import com.hangzhou.xyj.net.request.base.BaseRequest;
import com.hangzhou.xyj.utils.NetworkUtil;
import com.hangzhou.xyj.utils.StringUtil;


/**
 * Created by neo.duan on 2015/12/20.
 * http请求：单例模式
 */
public class HttpLoader extends BaseHttpLoader {
    private static class HttpLoaderHolder {
        private static final HttpLoader instance = new HttpLoader();
    }

    private HttpLoader() {
    }

    public static final HttpLoader getInstance() {
        return HttpLoaderHolder.instance;
    }

    public void sendRequest(BaseHttpHandler handler) {
        //校验handler处理对象是否为空，抛异常，调用者处理
        if (handler == null) {
            Log.e(TAG, "http handler object is not null");
            return;
        }

        //校验请求对象是否为空
        BaseRequest request = handler.getRequest();
        if (request == null) {
            Log.e(TAG, "http handler the request object is not null");
            return;
        }
        //校验url是否为空
        if (StringUtil.isBlank(request.getUrl())) {
            Log.e(TAG, "http handler object is not null");
            return;
        }
        //通过校验，调用请求开始
        handler.onStart();

        //真正发请求前，校验网络是否正常
        if (!NetworkUtil.isNetworkConnect()) {
            handler.onNetWorkErrorResponse();
            return;
        }

        //真正发请求
        super.setPostRequest(handler);
    }
}
